package edu.siu.math.egut.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import edu.siu.math.egut.io.LiE;
import edu.siu.math.egut.io.OutputPref;
import edu.siu.math.egut.main.OutputStyle;

/**
 * 
 * Static methods that do not belong to any particular class.
 * 
 * @author Joseph Hundley
 *
 */

public class Algorithms {
    
    /**
     * Returns true if any entry of its argument is negative and false otherwise.
     * This will, of course, accurately detect negative roots.  
     * @param a an int[]
     * @return true if any entry of its argument is negative and false otherwise.
     */
    public static boolean isNegative( final int[]a){
	for( int i = 0 ; i < a.length; i++ )
	    if( a[i] <0 )
		return true;
		
	return false;
    }
    
    /**
     * Checks whether a given string may be interpreted as a word in the simple reflections of 
     * the exceptional group of the specified rank.
      * @param w a String-- potentially representing a Weyl word
     * @param rank an integer.
     * @return true if every character in w is a digit between 1 and rank inclusive
     */
    public static boolean representsWeylWord(String w, int rank){
	for( int j=w.length()-1; j>-1; j-- ){
	    if((int) w.charAt(j)-48 <1 || (int) w.charAt(j)-48 > rank)
		return false;
	    // the magic number 48 has the property that 'd' -48 = d for any digit d.  (that is, the 
	    // integral value assigned to the digit d as a char is equal to d+48.)
	
	}
	return true;
	// so the effect is to return true iff every character in the string is a digit between 1 and rank inclusive.
 
    }

    /**
     * Finds the index of the given int[] in the given int[][]
     * @param roots an int[][]
     * @param root an int[]
     * @return the index of the int[] in the in
     */
    public static int indexOf(int[][] roots, int[] root) {
		for (int i = 0; i < roots.length; i++)
		    if (Arrays.equals(root, roots[i]))
			return i;
		return -1;
	    
    }

    public static int[][] preimage(WeylWord w, ArrayList<int[]> roots) throws IOException {
	LiE lie = new LiE();
	return edu.siu.math.egut.io.Algorithms.readLiEMatrix(
		edu.siu.math.egut.io.Algorithms.removeWhiteSpace(
		lie.exec(
			"preimage("+ w.toString(OutputStyle.LIE)+","+
			 edu.siu.math.egut.io.Algorithms.writeLieMatrix(roots) + ","+
			 w.getG().toString()+")" , 
			OutputPref.HIDE)));
    }

    
    
    
}// end class declaration "Algorithms"
